#include "Core/Log/LogManager.h"
#include "Core/File/File.h"
#include "Core/Profile/CodeProfile.h"
#include "FileTest.h"

const char * FileTest::GetName() const
{
	return "[CORE] File Test";
}

Core::error FileTest::Create()
{
	if(Failed(UnitTest::Create()))
		return Core::errFailed;

	return Core::errOK;
}

Core::error FileTest::Destroy()
{
	if(Failed(UnitTest::Destroy()))
		return Core::errFailed;

	return Core::errOK;
}

Core::error FileTest::Execute()
{
	PROFILE(GetName());


	// Text File
	{
		// write
		float fTest = 1.2548f;
		uint32 uTest = 8365781;
		int iTest = -842365;
		char cTest = 'u';

		Core::File::File oTextFile("../data/Tests/toto.txt");

		if (!oTextFile.Open(Core::File::eFile_Write | Core::File::eFile_Text))
			return Core::errFailed;

		oTextFile.Write("%f %d\n", fTest, iTest);
		oTextFile << fTest << " " << uTest << " " << iTest << cTest << '\n';

		if(! oTextFile.Close())
			return Core::errFailed;

		// read
		std::string str;
		float fReadTest;
		uint32 uReadTest;
		int iReadTest;
		char cReadTest;

		if (!oTextFile.Open(Core::File::eFile_Text | Core::File::eFile_Read))
			return Core::errFailed;

		oTextFile.ReadLine(str);
		oTextFile >> fReadTest >> uReadTest >> iReadTest >> cReadTest;

		if(fReadTest != fTest)
			return Core::errFailed;

		if(uReadTest != uTest)
			return Core::errFailed;

		if(iReadTest != iTest)
			return Core::errFailed;

		if(cReadTest != cTest)
			return Core::errFailed;

		if(! oTextFile.Close())
			return Core::errFailed;
	}
	// binary File
	{
		// write
		float fTest = 1.2548f;
		uint32 uTest = 8365781;
		int iTest = -842365;
		char cTest = 'u';

		Core::File::File oTextFile("../data/Tests/toto.dat");

		if (!oTextFile.Open(Core::File::eFile_Write | Core::File::eFile_Binary))
			return Core::errFailed;

		oTextFile << fTest << uTest << iTest << cTest;

		if(! oTextFile.Close())
			return Core::errFailed;

		// read
		std::string str;
		float fReadTest;
		uint32 uReadTest;
		int iReadTest;
		char cReadTest;

		if (!oTextFile.Open(Core::File::eFile_Binary | Core::File::eFile_Read))
			return Core::errFailed;

		oTextFile >> fReadTest >> uReadTest >> iReadTest >> cReadTest;

		if(fReadTest != fTest)
			return Core::errFailed;

		if(uReadTest != uTest)
			return Core::errFailed;

		if(iReadTest != iTest)
			return Core::errFailed;

		if(cReadTest != cTest)
			return Core::errFailed;

		if(! oTextFile.Close())
			return Core::errFailed;
	}


	return Core::errOK;
}
